// TODO: 数据结构
// DATE: 2022/3/18
// AUTHOR: Cheng Ze WUST
fun main(args: Array<String>){
//region 不可变list集合
    val list=listOf("Cheng","Lee","Orcal")

    //普通取值方式 重载了JAVA的get方法
    println(list[0])
    //防止下标越界崩溃：
    println(list.getOrElse(2){"越界"})
    println(list.getOrElse(3){"越界"})
    println(list.getOrNull(1))
    println(list.getOrNull(4))
//endregion

//region 可变list集合
    val listm= mutableListOf("Apple","Huawei","Xiaomi")
    listm.add("Vivo")
    listm.remove("Apple")
    println(listm)

    //复制为可变集合
    val lists : MutableList<String> = list.toMutableList()
    //复制为不可变集合
    val listt : List<String> = listm.toList()
//endregion

//region mutator
    listm+="Oppo"
    listm-="Vivo"
    //本质是运算符重载
    println(listm)

    listm.removeIf{ it.contains("o") }  //有“o”的全部删除
    println(listm)
//endregion

//region 遍历集合
    println("for循环遍历")
    for (i in lists) {
        print("$i  ")
    }
    println("\nforeach遍历")
    lists.forEach {
        print("$it  ")
    }
    println("\nforeach遍历带下标")
    list.forEachIndexed{index,item->
        print("$index : $item  ")
    }
//endregion

    println()
//region 固定set集合
    val setList:Set<Int> = setOf(21,28,16,72,21)
    //set无重复列表
    println(setList)
    println(setList.elementAt(0))
    println(setList.elementAtOrElse(4){"越界"})
    println(setList.elementAtOrNull(5))
//endregion

//region 可变set集合
    val setListM: MutableList<Int> = mutableListOf(12,30,16)
    setListM+=8
    setListM-=12
    println(setListM)
//endregion

//region 集合转换
    val list2Set = list.toSet()
    println(list.toSet().toList())
    println(list.distinct())    //快速去除重复元素
//endregion

//region Map
    val map1:Map<String,Int> = mapOf("Zhang" to 12,"Lee" to 13,"Jiang" to 15)
    val map2= mapOf(Pair("Wang",2), Pair("Jun",4))
    println(map1)
    println(map2["Jun"])
    println(map1.getOrDefault("Miky",-1))
    println(map2.getOrElse("July"){"没找到"})

    //两种遍历方式
    map1.forEach{
        println("${it.key} x ${it.value}")
    }
    for (entry in map2) {
        println("${entry.key}  *  ${entry.value}")
    }

    //可变Map集合
    val map3:MutableMap<String,Int> = mutableMapOf("Sheng" to 8,"Zhao" to 9,"Cheng" to 10)
    map3+="Sun" to 12
    map3-="Zhao"
    map3["Sheng"]=6
    println(map3)
//endregion




}